import React ,{ Component }from 'react';
import { Menu } from 'antd';
import './index.scss';
import { connect } from 'react-redux';
import * as actionCreators from '../../store/actionCreators';
import {withRouter } from 'react-router-dom'
const { SubMenu } = Menu;

class Index extends Component {
    constructor(props) {
        super(props);
        this.renderDom = this.renderDom.bind(this);
        this.linkTo = this.renderDom.bind(this);
    }

    componentDidMount() {
        console.log(this.props.loginUser)
    }

    navigateTo(item) {
        this.props.history.push(item.url);
    }

    renderDom(item) {
        if (!item.childPrivilege || item.childPrivilege.length === 0) {
            return <Menu.Item key={item.privilegeId} onClick={() => this.navigateTo(item)}>
                <span onClick={() => this.navigateTo(item)}>{item.privilegeName}</span>
            </Menu.Item>;
        } else {
            return <SubMenu
                key={item.privilegeId}
                title={<span>{item.privilegeName}</span>}>
                {
                    item.childPrivilege.map((cItem,index) => {
                        return <Menu.Item key={cItem.privilegeId} onClick={() => this.navigateTo(cItem)}>
                            <span onClick={() => this.navigateTo(cItem)}>{cItem.privilegeName}</span>
                        </Menu.Item>
                    })
                }
            </SubMenu>
        }
    }

    render() {
        let priList = [
            {
                id: null,
                creator: null,
                createTime: null,
                updateTime: null,
                privilegeId: "8SgR63tn0D5OTTnquLAKzZBcC",
                privilegeName: "系统管理",
                code: "SYS_XTGL",
                description: null,
                targetId: "3ZfyyFUFzRg00dyy2rf38a1o0",
                url: "/index",
                style: "gear-b",
                roleId: "140SFUHoqWwsgmPFZ3AdzWLas",
                childPrivilege: [
                    {
                        id: null,
                        creator: null,
                        createTime: null,
                        updateTime: null,
                        privilegeId: "zBq1YcahPOtbRrpPKCLwTGh5M",
                        privilegeName: "菜单管理",
                        code: "SYS_XTGL_CDGL",
                        description: null,
                        targetId: "XMIpt5i9U84MMmR9WPctct4Py",
                        url: "/index/menu",
                        style: "",
                        roleId: null,
                        childPrivilege: null
                    },
                    {
                        id: null,
                        creator: null,
                        createTime: null,
                        updateTime: null,
                        privilegeId: "7EsF4vUiTK8tz58xFHuh6H4mV",
                        privilegeName: " HOME",
                        code: "SYS_XTGL_BZDLXGL",
                        description: null,
                        targetId: "OePPSouYXYFUJHvmFE8QkH7oZ",
                        url: "/index/home",
                        style: "",
                        roleId: null,
                        childPrivilege: null
                    }
                ]
            },
            {
                id: null,
                creator: null,
                createTime: null,
                updateTime: null,
                privilegeId: "1111",
                privilegeName: "HOME主頁",
                code: "SYS_XTGL_CDGL",
                description: null,
                targetId: "XMIpt5i9U84MMmR9WPctct4Py",
                url: "/home",
                style: "",
                roleId: null,
                childPrivilege: null
            }
        ];

        let menuItems = <Menu
            mode="inline"
            className="menu-wrapper"
            theme="dark">
            {
                priList.map((item, index) => {
                    return this.renderDom(item);
                })
            }
        </Menu>;
        return (
            <React.Fragment>
                {menuItems}
            </React.Fragment>
        );
    }
}
const mapStateToProps = (state) => {
    return {
        loginUser: state.get('loginUser')
    }
}

const mapDispatchToProps = (dispatch) => ({
    setLoginUser(data) {
        const action = actionCreators.getLoginUser(data);
        dispatch(action);
    }
})
export default connect(mapStateToProps,mapDispatchToProps)(withRouter(Index));
